#ifndef MSG_RECEIVE_H
#define MSG_RECEIVE_H
#include "BF7006AMxx.h"


typedef enum 
{
	DECODE_BIT_0 = 0,
	DECODE_BIT_1 = 1,
	DECODE_UNCOMPLETE = -1,
	DECODE_ERROR     = -2,
	DECODE_STOP     = -3,
}DECODE_BIT_STATUS;


typedef struct bit_decode_flag_{
	uint8_t p_flg;
	uint8_t h_flg;
	#define START_BIT_P       1
	#define PARITY_BIT_P      10
	#define STOP_BIT_P        11
	#define BYTE_ENCODE_LENTH 11
	uint8_t h_count;
	uint8_t m_flg;
	uint16_t m_count;
	uint8_t c_flg;	
	uint8_t c_count;
	#define PREMBLE_ERROR   (0x01<<0)
	#define HEADER_ERROR    (0x01<<1)
	#define MESSAGE_ERROR	(0x01<<2)
	#define CHECKSUM_ERROR  (0x01<<3)
	uint8_t error;
}bit_flag_t;

typedef struct bit_buffer_{
	#define MAX_COUNT	10
	#define BUFF_LENGTH  32
	uint8_t buff[MAX_COUNT][BUFF_LENGTH];
	uint8_t index[MAX_COUNT];
	uint16_t length[MAX_COUNT];
	
	#define STATUS_RESET 0
	#define STATUS_BUSY  1
	#define STATUS_COMPLETE 2	
	uint8_t status;
}bit_buff_t;


typedef struct message_body_t{
	uint32_t premble;
	uint8_t header;
	uint8_t h_parity;
	#define MAX_MESSAGE_BYTE 27
	uint8_t message[MAX_MESSAGE_BYTE];
	uint8_t msg_size;
	uint8_t m_parity;
	uint8_t checksum;	
	uint8_t c_parity;
}msg_body_t;

typedef struct receive_message_t{
	msg_body_t msg;
	bit_flag_t flag;
}rec_msg_t;


typedef struct rec_msg_fifo_t{
	#define REC_MSG_FIFO_MAX 32
	msg_body_t fifo[REC_MSG_FIFO_MAX];
	uint8_t r_index;
	uint8_t w_index;
	#define FIFO_NONEMPTY  1
	#define FIFO_EMPTY 0
	uint8_t fifo_flag;
}msg_fifo_t;


void update_bit_decoder_status(uint8_t status);
void bit_data_handle(void);

void enable_sc5003_vdmo_interrupt(void);
void disable_sc5003_vdmo_interrupt(void);
uint8_t read_msg_from_fifo(msg_body_t *msg);
void ask_timer_mod_count_updata(void);
#endif
